package com.hos.utile;

/**
 * Created by guom on 2018/11/11.
 */
public class NumberUtile {
    /**
     * String转int
     */
    public static int getStringToInt(String number) {
        return getStringToInt(number, -1);
    }

    /**
     * String转int
     */
    public static int getStringToInt(String number, int defaultInt) {
        if (StringUtile.isEmpty(number)) {
            return defaultInt;
        }
        try {
            return Integer.parseInt(number);
        } catch (Exception e) {
            return defaultInt;
        }
    }

    /**
     * String转long
     */
    public static long getStringToLong(String number) {
        return getStringToLong(number, 0);
    }

    /**
     * String转long
     */
    public static long getStringToLong(String number, long defaultInt) {
        if (StringUtile.isEmpty(number)) {
            return defaultInt;
        }
        try {
            return Long.parseLong(number);
        } catch (Exception e) {
            return defaultInt;
        }
    }


    /**
     * String转Float
     */
    public static float getStringToFloat(String number) {
        return getStringToFloat(number, 0);
    }

    /**
     * String转Float
     */
    public static float getStringToFloat(String number, float def) {
        if (StringUtile.isEmpty(number)) {
            return def;
        }
        try {
            return Float.parseFloat(number);
        } catch (Exception e) {
            return def;
        }
    }

    /**
     * String转double
     */
    public static double getStringToDouble(String number) {
        return getStringToDouble(number, -1);
    }

    /**
     * String转String转double
     */
    public static double getStringToDouble(String number, double defaultInt) {
        if (StringUtile.isEmpty(number)) {
            return defaultInt;
        }
        try {
            return Double.parseDouble(number);
        } catch (Exception e) {
            return defaultInt;
        }
    }
}
